#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <db.h>


#define DBNAME "test2.bdb"


typedef struct emp
{
    int empid;
    char lastname[50];
    char firstname[50];
    float salary;
} emp_t;


DB *openDB(const char *dbFile)
{
    DB *dbp = NULL;
    int ret = 0;


    ret = db_create(&dbp, NULL, 0);
    if (ret != 0)
    {
        printf("ERROR: %s\n",db_strerror(ret));
        return NULL;
    }
    ret = dbp->open(dbp, NULL, dbFile, NULL, DB_BTREE, DB_CREATE, 0);
    if (ret != 0)
    {
        printf("ERROR: %s\n",db_strerror(ret));
        return NULL;
    }
    return dbp;
}


int main(int argc, char* argv[])
{
    DB *dbp;
    DBC *cursor;
    int ret = 0;
    DBT key, data;
    emp_t emp;
    if ((dbp = openDB(DBNAME)) == NULL)
    {
        printf("open failed\n");
        return 1;
    }


    /* insert */
    while (1)
    {
        printf("Enter Employee ID (0 is quit): ");
        scanf("%d", &emp.empid);
        if (emp.empid == 0)
            break;
        printf("Enter Last name: ");
        scanf("%s", emp.lastname);
        printf("Enter first name: ");
        scanf("%s", emp.firstname);
        printf("Enter Salary: ");
        scanf("%f", &emp.salary);


        memset(&key, 0, sizeof(DBT));
        memset(&data, 0, sizeof(DBT));


        key.data = &(emp.empid);
        key.size = sizeof(emp.empid);
        data.data = &emp;
        data.size = sizeof(emp_t);


        ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
        if (ret != 0)
        {
            printf("Employee ID exists\n");
            continue;
        }
    }


    /* search */
    while (1)
    {
        printf("Enter Employee ID you wana search(0 is quit): ");
        scanf("%d", &emp.empid);
        if (emp.empid == 0)
            break;
        memset(&key, 0, sizeof(DBT));
        memset(&data, 0, sizeof(DBT));


        key.data = &(emp.empid);
        key.size = sizeof(emp.empid);
        data.data = &emp;
        data.ulen = sizeof(emp_t);
        data.flags = DB_DBT_USERMEM;
        ret = dbp->get(dbp, NULL, &key, &data, 0);
        if (ret != 0)
        {
            printf("Employee ID doesn't exist\n");
        }
        else
        {
            printf("  Employee: %d - %s, %s\n  Salary: $%.2lf\n", emp.empid, emp.lastname, emp.firstname, emp.salary);
        }
    }


    /* delete */
    while (1)
    {
        printf("Enter Employee ID you wana delete(0 is quit): ");
        scanf("%d", &emp.empid);
        if (emp.empid == 0)
            break;
        memset(&key, 0, sizeof(DBT));
        memset(&data, 0, sizeof(DBT));


        key.data = &(emp.empid);
        key.size = sizeof(emp.empid);


        ret = dbp->del(dbp, NULL, &key, 0);
        if (ret != 0)
        {
            if (ret == DB_NOTFOUND)
                printf("Employee ID doesn't exist\n");
        }
        else
        {
            printf("  Employee %d deleted.\n", emp.empid);
        }


    }


    /* cursor */
    ret = dbp->cursor(dbp, NULL, &cursor, 0);
    if (ret != 0)
    {
        printf("ERROR: %s\n",db_strerror(ret));
        return 1;
    }
    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));
    data.data = &emp;
    data.ulen = sizeof(emp);
    data.flags = DB_DBT_USERMEM;
    /* data.flags = DB_DBT_USERMEM; */


    while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0)
    {
        printf("ret = %d\n", ret);
        printf("%d - %s, %s    $%.2lf\n", emp.empid, emp.lastname, emp.firstname, emp.salary);
        memset(&key, 0, sizeof(DBT));
        memset(&data, 0, sizeof(DBT));
    }
    if (ret < 0)
    {
        printf("ERROR: %s\n",db_strerror(ret));
    }
    cursor->close(cursor);


    dbp->close(dbp, 0);


    return 0;
}
